$(function() {

	// document.writeln("<script type=\"text/javascript\" " +
	// "src=\"js/jquery-3.2.1.min.js\"><\/script>");

	var ope = new MUserOperation();

	$("input[type=file]").change(function() {
		$(this).parents(".uploader").find(".filename").val($(this).val());
	});
	$("input[type=file]").each(
			function() {
				if ($(this).val() == "") {
					$(this).parents(".uploader").find(".filename").val(
							"No file selected...");
				}
			});
	// 注册表单提交--监听
	$("#registForm").on(
			"submit",
			function(e) {
				e.preventDefault();
				var name = $("input[name=name]").val();
				var pwd = $("input[name=pwd]").val();
				var storename = $("input[name=storename]").val();
				var address = $("input[name=address]").val();
				console.log(name)
				if (name.length == 0 || pwd.length == 0
						|| storename.length == 0 || address.length == 0) {
					swal({
						title : "Fields are empty!",
						type : "error",
						timer : 1000,
						showConfirmButton : true,
						confirmButtonColor : "#66CD00",
					});
					return;
				}

				var fileId = [ 'idcard', 'img' ];
				var MUserVo = {

					name : name,
					pwd : pwd,
					storename : storename,
					address : address
				};
				ope.regist(MUserVo, fileId);
			});

	// 登陆按钮点击--监听
	$("#login").on("click", function(e) {
		e.preventDefault();

		var name = $("input[name=name]").val();
		var pwd = $("input[name=pwd]").val();
		if (name.length == 0 || pwd.length == 0) {
			swal({
				title : "Fields are empty!",
				type : "error",
				timer : 1000,
				showConfirmButton : true,
				confirmButtonColor : "#66CD00",
			});
			return;
		}

		var MUserVo = {
			name : name,
			pwd : pwd,
			storename : "",
			address : ""
		};
		ope.login(MUserVo);
	});

	// 更新按钮监听 打开模态框
	$("#openUpdate").on("click", function() {

		var name = $("#td1").text();
		var storename = $("#td2").text();
		var addr = $("#td3").text();

		console.log(name)
		$("input[name=updateName]").val(name);
		$("input[name=updateAddr]").val(addr);
		$("input[name=updateStoreName]").val(storename);
		$('#updateModal').modal('show')

	});

	// 模态框更新按钮监听
	$("#modalBtn").on("click", function() {

		var name = $("input[name=updateName]").val();
		var addr = $("input[name=updateAddr]").val();
		var store_name = $("input[name=updateStoreName]").val();

		var updateFileId = [ 'updateIacard', 'updateImg' ];
		var updateMUserVo = {

			updateName : "",
			pwd : "",
			updateStoreName : store_name,
			updateAddr : addr
		};
		ope.update(updateMUserVo, updateFileId);
	})

});

// 对 merchant的操作
function MUserOperation() {

	// 注册回调
	this.regist = function(MUserVo, fileId) {

		up.upload("/meat/muser/regist", MUserVo, fileId, function(callback) {
			if (callback.code == 200) {
				$(location).attr('href', 'login.jsp');
			} else if (callback.code = 404) {

				swal({
					title : "Please input again",
					text : callback.msg,
					type : "error",
					timer : 1000,
					showConfirmButton : true,
					confirmButtonColor : "#66CD00",
				});
			} else {
				$(location).attr('href', 'block.jsp');
			}
		}, function(callback) {
			swal({
				title : "Server error",
				text : callback.msg,
				type : "error",
				timer : 1000,
				showConfirmButton : true,
				confirmButtonColor : "#66CD00",
			});
		});
	}

	// 更新回调
	this.update = function(updateMUserVo, updateFileId) {

		console.log(updateMUserVo)
		up.upload("/meat/sec/muser/update", updateMUserVo, updateFileId,
				function(callback) {
					if (callback.code == 200) {
						var m = callback.data;
						$('#updateModal').modal('hide');
						$("#td1").text(m.name);
						$("#td2").text(m.store_name);
						$("#td3").text(m.address);
						$("#img-circle").attr("src",
								m.img + "?t=" + Date.parse(new Date()));
//						$("#img-circle").attr("src",function(){return m.img + "?t=" + Date.parse(new Date())});
//						swal({
//							title : "Update success",
//							text : callback.msg,
//							type : "success",
//							timer : 1000,
//							showConfirmButton : true,
//							confirmButtonColor : "#66CD00",
//						});
						window.location.reload();
					} else if (callback.code = 404) {

						swal({
							title : "Please input again",
							text : callback.msg,
							type : "error",
							timer : 1000,
							showConfirmButton : true,
							confirmButtonColor : "#66CD00",
						});
					} else {
						$(location).attr('href', 'block.jsp');
					}
				}, null);

	}

	// 登陆回调
	this.login = function(MUserVo) {

		up.ajax("POST", "/meat/muser/login", MUserVo, {
			"content-Type" : "application/json"
		}, this, function(callback) {
			if (callback.code == 404) {
				swal({
					title : "Please input again",
					text : callback.msg,
					type : "error",
					timer : 1000,
					showConfirmButton : true,
					confirmButtonColor : "#66CD00",
				});
			} else if (callback.code == 200) {
				var state = callback.data.state;
				var block = callback.data.block;

				
				if (block == 1) {// block
					$(location).attr('href', 'block.jsp');
				} else {
					if (state == 1) {// enter management page
						$(location)
								.attr('href', 'sec/manage/manage_order.jsp');
					} else {
						$(location).attr('href', 'sec/muser/info.jsp');
					}console.log(callback.data)
				}
			} else {
				$(location).attr('href', 'error.jsp');
			}
		});
	}

};

(function() {// 一次函数
	var obj = {};

	// 引入js
	// obj.include=function(path){
	//		
	// var a=document.createElement("script");
	// a.type = "text/javascript";
	// a.src=path;
	// var head=document.getElementsByTagName("head")[0];
	// head.appendChild(a);
	// };

	// 封装AjaxFileUpload
	obj.upload = function(url, datas, filesID, sucCallback, errorCallback) {

		$.ajaxFileUpload({
			url : url,// servlet请求路径
			secureuri : false,
			fileElementId : filesID,// 上传控件的id
			dataType : 'json',
			data : datas, // 其它请求参数
			success : function(data, status) {
				sucCallback(data)
			},
			error : function(data, status, e) {
				errorCallback(data)
			}
		});
	};

	// 封装Ajax
	obj.ajax = function(type, url, data, headers, context, callback, callback1) {

		$.ajax({
			url : url,
			type : type,
			data : JSON.stringify(data),
			headers : headers,
			context : context
		// 回调函数的this
		}).done(callback).fail(callback1)
	};

	window.up = obj;

})();